//package com.youbang.base.shiro;
//
//
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.util.ByteSource;
//
//import com.youbang.bsMgr.permission.model.PermissionEntity;
//import com.youbang.bsMgr.permission.service.PermissionService;
//import com.youbang.bsMgr.user.model.UserEntity;
//import com.youbang.bsMgr.user.service.UserService;
//
//import javax.annotation.Resource;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//
//public class MyShiroRealm extends AuthorizingRealm {
//
//    @Resource
//    private UserService userService;
//
//    @Resource
//    private PermissionService permissionService;
//
//    //授权
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        UserEntity user= (UserEntity) SecurityUtils.getSubject().getPrincipal();//UserEntity{id=1, username='admin', password='3ef7164d1f6167cb9f2658c07d3c2f0a', enable=1}
//        Map<String,Object> map = new HashMap<String,Object>();
//        map.put("userid",user.getId());
////        List<PermissionEntity> resourcesList = permissionService.loadUserEntityPermission(map);
//        // 权限信息对象info,用来存放查出的用户的所有的角色（role）及权限（permission）
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
////        for(PermissionEntity resources: resourcesList){
////            info.addStringPermission(resources.getResurl());
////        }
//        return info;
//    }
//
//    //认证
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        //获取用户的输入的账号.
//        String loginId = (String)token.getPrincipal();
//        UserEntity user = userService.queryEntityByLoginId(loginId);
//        if(user==null) throw new UnknownAccountException();
////        if (0==user.getEnable()) {
////            throw new LockedAccountException(); // 帐号锁定
////        }
//        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
//                user, //用户
//                user.getPasswd(), //密码
//                ByteSource.Util.bytes(loginId),
//                getName()  //realm name
//        );
//        // 当验证都通过后，把用户信息放在session里
//        Session session = SecurityUtils.getSubject().getSession();
//        session.setAttribute("userSession", user);
//        session.setAttribute("userSessionId", user.getId());
//        return authenticationInfo;
//    }
//
//
//}